<?php
header('Content-type:text/html;charset=utf-8');
//在被包含页面不要再包含文件
//包含数据库配置文件
include '../../public/dbconfig.php';

//包含数据库文件
include '../../public/func.inc.php';
//连接数据库
$link = conn();
//var_dump($link);

//接受用户信息  验证 存储到用户表中
//var_dump($_POST);
$uname = $_POST['uname'];
$pass = $_POST['pass'];
$repass = $_POST['repass'];
$qx = $_POST['qx'];
//验证用户是否已存在
$sql1 = "select username from user where username='{$uname}'";
//var_dump($sql1);
//exit;
$res1 = mysqli_query($link, $sql1);
$num = mysqli_num_rows($res1);
if($num > 0 ){
	echo '<script>alert("用户名已存在");window.location.href="add.html";</script>';
	exit;
}
//验证  正则 字符串相关函数
//1 用户名长度不能小于6
$len = strlen($uname);
if($len < 6){
	echo '<script>alert("用户名长度不能小于6");window.location.href="add.html";</script>';
	exit;
}


if($pass != $repass){
	echo '<script>alert("密码与确认密码不一致");window.location.href="add.html";</script>';
	exit;
	
}else{
	$newpass = md5($pass);
	
}

//获取注册时间
$rtime = time();

//获取注册IP
$rip = $_SERVER['REMOTE_ADDR'];
//var_dump($rip);
//exit;


if($rip == '::1'){
	$rip = '127.0.0.1';
}
//数据表中的ip为int
$longip =ip2long($rip);
//var_dump($longip);
//exit;


//准备添加用户sql 语句
$sql = "insert into user(username,password,rtime,rip,qx) values('{$uname}','{$newpass}','{$rtime}','{$longip}','{$qx}')";
//var_dump($sql);
//exit;
//echo $sql;

//执行添加用户操作
$res = mysqli_query($link,$sql);

//var_dump($res);
//exit;
if($res){
	echo  '<script>alert("恭喜,添加成功");window.location.href="list.php";</script>';
}else{
	echo '<script>alert("添加失败");window.location.href="add.html";</script>';
}

//关闭数据库连接

mysqli_close($link);



























